#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import config as c
from gitlab_map import site_map
import os
import re

root_path = c.local_www_path
remove_folder_path = 'kindeditor'
remove_file_path = 'site_admintAG8A4VZ/kindeditor/php/demo.php'


def remove_folder_handler(project, remove_folder, root):
    try:
        # 判断目录是否存在
        full_path = root + project + '/' + remove_folder
        is_exists = os.path.exists(full_path)

        if is_exists:
            cmd = "cd %s && git rm -r --cached %s" % (root + project, remove_folder)
            print("执行语句 >>> ", cmd)
            os.system(cmd)
        else:
            print(project + ', ' + remove_folder + ' ***** 文件夹不存在 *****')
            pass
    except Exception as e:
        print(remove_folder + ' ***** 异常 ***** >>> ' + str(e))


def remove_file_handler(project, remove_file, root):
    try:
        # 判断文件是否存在
        full_path = root + project + '/' + remove_file
        is_exists = os.path.exists(full_path)

        if is_exists:
            cmd = "cd %s && git rm %s" % (root + project, remove_file)
            print("执行语句 >>> ", cmd)
            os.system(cmd)
        else:
            print(project + ', ' + remove_file + ' ***** 文件不存在 *****')
            pass
    except Exception as e:
        print(remove_file + ' ***** 异常 ***** >>> ' + str(e))


if __name__ == "__main__":
    for i in c.dirname_list:
        i = i.lower()
        dirname = site_map[i][35:][:-4].lower()
        if remove_folder_path != '':
            remove_folder_handler(dirname, remove_folder_path, root_path)
        if remove_file_path != '':
            remove_file_handler(dirname, remove_file_path, root_path)
